package com.cho3.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class MatchesAdapter extends PagerAdapter {

    private List<View> matches;
    private onItemClickListener mOnItemClickListener;

    public MatchesAdapter(ArrayList data) {
        this.matches = data;
    }

    @Override
    public int getCount() {
        return matches.size();
    }

    @Override
    public boolean isViewFromObject( View view,  Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem( ViewGroup container, final int position) {
        View view = matches.get(position);
        view.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position);
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem( ViewGroup container, int position,  Object object) {
        container.removeView(matches.get(position));
    }

    public interface onItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
